{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 腾讯云—人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'ret': 0,\n",
       " 'msg': 'ok',\n",
       " 'data': {'image_width': 486,\n",
       "  'image_height': 527,\n",
       "  'face_list': [{'face_id': '3731014840708273357',\n",
       "    'x': 176,\n",
       "    'y': 90,\n",
       "    'width': 154,\n",
       "    'height': 154,\n",
       "    'gender': 0,\n",
       "    'age': 21,\n",
       "    'expression': 51,\n",
       "    'beauty': 100,\n",
       "    'glass': 0,\n",
       "    'pitch': 24,\n",
       "    'yaw': -4,\n",
       "    'roll': 0,\n",
       "    'face_shape': {'face_profile': [{'x': 185, 'y': 144},\n",
       "      {'x': 186, 'y': 156},\n",
       "      {'x': 188, 'y': 169},\n",
       "      {'x': 192, 'y': 181},\n",
       "      {'x': 197, 'y': 192},\n",
       "      {'x': 203, 'y': 203},\n",
       "      {'x': 210, 'y': 213},\n",
       "      {'x': 219, 'y': 223},\n",
       "      {'x': 227, 'y': 232},\n",
       "      {'x': 237, 'y': 239},\n",
       "      {'x': 249, 'y': 243},\n",
       "      {'x': 262, 'y': 241},\n",
       "      {'x': 273, 'y': 234},\n",
       "      {'x': 282, 'y': 226},\n",
       "      {'x': 292, 'y': 217},\n",
       "      {'x': 300, 'y': 206},\n",
       "      {'x': 306, 'y': 195},\n",
       "      {'x': 311, 'y': 183},\n",
       "      {'x': 315, 'y': 170},\n",
       "      {'x': 317, 'y': 157},\n",
       "      {'x': 318, 'y': 146}],\n",
       "     'left_eye': [{'x': 202, 'y': 145},\n",
       "      {'x': 207, 'y': 150},\n",
       "      {'x': 213, 'y': 152},\n",
       "      {'x': 220, 'y': 153},\n",
       "      {'x': 227, 'y': 151},\n",
       "      {'x': 223, 'y': 144},\n",
       "      {'x': 216, 'y': 140},\n",
       "      {'x': 209, 'y': 140}],\n",
       "     'right_eye': [{'x': 290, 'y': 146},\n",
       "      {'x': 284, 'y': 150},\n",
       "      {'x': 277, 'y': 153},\n",
       "      {'x': 270, 'y': 153},\n",
       "      {'x': 263, 'y': 151},\n",
       "      {'x': 267, 'y': 143},\n",
       "      {'x': 275, 'y': 140},\n",
       "      {'x': 283, 'y': 141}],\n",
       "     'left_eyebrow': [{'x': 187, 'y': 131},\n",
       "      {'x': 198, 'y': 133},\n",
       "      {'x': 208, 'y': 135},\n",
       "      {'x': 219, 'y': 137},\n",
       "      {'x': 230, 'y': 138},\n",
       "      {'x': 220, 'y': 131},\n",
       "      {'x': 209, 'y': 128},\n",
       "      {'x': 197, 'y': 126}],\n",
       "     'right_eyebrow': [{'x': 304, 'y': 129},\n",
       "      {'x': 292, 'y': 131},\n",
       "      {'x': 279, 'y': 133},\n",
       "      {'x': 267, 'y': 136},\n",
       "      {'x': 255, 'y': 138},\n",
       "      {'x': 265, 'y': 130},\n",
       "      {'x': 278, 'y': 126},\n",
       "      {'x': 291, 'y': 124}],\n",
       "     'mouth': [{'x': 224, 'y': 205},\n",
       "      {'x': 229, 'y': 212},\n",
       "      {'x': 237, 'y': 218},\n",
       "      {'x': 246, 'y': 221},\n",
       "      {'x': 256, 'y': 218},\n",
       "      {'x': 265, 'y': 212},\n",
       "      {'x': 272, 'y': 204},\n",
       "      {'x': 261, 'y': 206},\n",
       "      {'x': 251, 'y': 206},\n",
       "      {'x': 245, 'y': 208},\n",
       "      {'x': 239, 'y': 206},\n",
       "      {'x': 231, 'y': 206},\n",
       "      {'x': 231, 'y': 208},\n",
       "      {'x': 238, 'y': 211},\n",
       "      {'x': 245, 'y': 213},\n",
       "      {'x': 254, 'y': 211},\n",
       "      {'x': 263, 'y': 208},\n",
       "      {'x': 263, 'y': 207},\n",
       "      {'x': 254, 'y': 209},\n",
       "      {'x': 245, 'y': 211},\n",
       "      {'x': 238, 'y': 210},\n",
       "      {'x': 231, 'y': 207}],\n",
       "     'nose': [{'x': 243, 'y': 190},\n",
       "      {'x': 244, 'y': 151},\n",
       "      {'x': 241, 'y': 161},\n",
       "      {'x': 237, 'y': 170},\n",
       "      {'x': 233, 'y': 180},\n",
       "      {'x': 229, 'y': 189},\n",
       "      {'x': 236, 'y': 194},\n",
       "      {'x': 244, 'y': 197},\n",
       "      {'x': 253, 'y': 195},\n",
       "      {'x': 262, 'y': 189},\n",
       "      {'x': 258, 'y': 180},\n",
       "      {'x': 253, 'y': 170},\n",
       "      {'x': 249, 'y': 161}]}}]}}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import hashlib  \n",
    "import time  \n",
    "import random  \n",
    "import string\n",
    "import requests  \n",
    "import base64  \n",
    "import requests\n",
    "#import cv2\n",
    "import numpy as np\n",
    "from urllib.parse import urlencode\n",
    "import json #用于post后得到的字符串到字典的转换\n",
    "\n",
    "app_id = '1106649312' \n",
    "app_key = 'TwsQQv5G5c9E6FsH'\n",
    "\n",
    "'''\n",
    "'''\n",
    "def get_params(img):                         #鉴权计算并返回请求参数\n",
    "    #请求时间戳（秒级），用于防止请求重放（保证签名5分钟有效\n",
    "    time_stamp=str(int(time.time())) \n",
    "    #请求随机字符串，用于保证签名不可预测,16代表16位\n",
    "    nonce_str = ''.join(random.sample(string.ascii_letters + string.digits, 16))\n",
    "\n",
    "    params = {'app_id':app_id,                #请求包，需要根据不同的任务修改，基本相同\n",
    "              'image':img,                    #文字类的任务可能是‘text’，由主函数传递进来\n",
    "              'mode':'0' ,                    #身份证件类可能是'card_type'\n",
    "              'time_stamp':time_stamp,        #时间戳，都一样\n",
    "              'nonce_str':nonce_str,          #随机字符串，都一样\n",
    "              #'sign':''                      #签名不参与鉴权计算，只是列出来示意\n",
    "             }\n",
    "\n",
    "    sort_dict= sorted(params.items(), key=lambda item:item[0], reverse = False)  #字典排序\n",
    "    sort_dict.append(('app_key',app_key))   #尾部添加appkey\n",
    "    rawtext= urlencode(sort_dict).encode()  #urlencod编码\n",
    "    sha = hashlib.md5()    \n",
    "    sha.update(rawtext)\n",
    "    md5text= sha.hexdigest().upper()        #MD5加密计算\n",
    "    params['sign']=md5text                  #将签名赋值到sign\n",
    "    return  params                          #返回请求包\n",
    "\n",
    "\n",
    "with open(r\"C:\\Users\\user\\Desktop\\one.jpg\",\"rb\") as f:\n",
    "    # b64encode是编码，b64decode是解码\n",
    "    base64_data = base64.b64encode(f.read())\n",
    "    # base64.b64decode(base64data)\n",
    "    \n",
    "# base64_data =pic_str\n",
    "\n",
    "#用opencv读入图片\n",
    "# frame=cv2.imread('e:/python/dlib/r3.jpg')\n",
    "# nparry_encode = cv2.imencode('.jpg', frame)[1]\n",
    "# data_encode = np.array(nparry_encode)\n",
    "# img = base64.b64encode(data_encode)    #得到API可以识别的字符串\n",
    "\n",
    "params = get_params(base64_data)    #获取鉴权签名并获取请求参数\n",
    "\n",
    "url = \"https://api.ai.qq.com/fcgi-bin/face/face_detectface\"  # 人脸分析\n",
    "#检测给定图片（Image）中的所有人脸（Face）的位置和相应的面部属性。位置包括（x, y, w, h），面部属性包括性别（gender）, 年龄（age）, 表情（expression）, 魅力（beauty）, 眼镜（glass）和姿态（pitch，roll，yaw）   \n",
    "res = requests.post(url,params).json()\n",
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
